<template>
  <div>
    <el-button type="primary" @click="showAdd = true">添加讲堂</el-button>
    <el-table :data="lectureList" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="60"/>
      <el-table-column prop="title" label="讲堂标题"/>
      <el-table-column prop="speaker" label="主讲人"/>
      <el-table-column prop="lectureTime" label="讲座时间"/>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="editLecture(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteLecture(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="showAdd" title="健康讲堂">
      <el-form :model="form">
        <el-form-item label="讲堂标题"><el-input v-model="form.title"/></el-form-item>
        <el-form-item label="主讲人"><el-input v-model="form.speaker"/></el-form-item>
        <el-form-item label="讲座时间"><el-date-picker v-model="form.lectureTime" type="datetime"/></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAdd = false">取消</el-button>
        <el-button type="primary" @click="saveLecture">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { getLectureListApi, addLectureApi, updateLectureApi, deleteLectureApi } from '../api/lecture'
const lectureList = ref([])
const showAdd = ref(false)
const form = ref({})
const loadLectures = async () => { lectureList.value = await getLectureListApi() }
const saveLecture = async () => {
  if(form.value.id) await updateLectureApi(form.value)
  else await addLectureApi(form.value)
  showAdd.value = false
  loadLectures()
}
const editLecture = (row) => { form.value = { ...row }; showAdd.value = true }
const deleteLecture = async (id) => { await deleteLectureApi(id); loadLectures() }
onMounted(loadLectures)
</script> 